<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Tag;
use App\Models\Article;

class TagController extends Controller
{
    public function index()
    {
        // 所有标签
        $tags = Tag::select('id', 'name')->withCount('articles')->get();

        $articles = Article::select()->orderBy('created_at', 'desc')->take(7)->get();
        return view('tag', ['tags' => $tags, 'articles' => $articles]);
    }

    public function detail(Request $request, $id)
    {
        // 所有标签
        $tags = Tag::select('id', 'name')->withCount('articles')->get();

        // 标签为id下的所有文章
        $articles = Tag::find($id)->articles;
        return view('tag', ['tags' => $tags, 'articles' => $articles]);
    }
}
